package com.yunhai.web.controller.file;

import com.yunhai.common.annotation.Log;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.common.enums.BusinessType;
import com.yunhai.common.utils.poi.ExcelUtil;
import com.yunhai.system.domain.Report;
import com.yunhai.system.service.IReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 报告管理Controller
 *
 * @author jxj
 * @date 2021-01-21
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/file/report")
public class ReportController extends BaseController {

    private final IReportService iReportService;

    /**
     * 查询报告管理列表
     */
    @PreAuthorize("@ss.hasPermi('file:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(Report report) {
        startPage();
        List<Report> list = iReportService.queryList(report);
        return getDataTable(list);
    }

    /**
     * 导出报告管理列表
     */
    @PreAuthorize("@ss.hasPermi('file:report:export')")
    @Log(title = "报告管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Report report) {
        List<Report> list = iReportService.queryList(report);
        ExcelUtil<Report> util = new ExcelUtil<Report>(Report.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 获取报告管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iReportService.getById(id));
    }

    /**
     * 新增报告管理
     */
    @PreAuthorize("@ss.hasPermi('file:report:add')")
    @Log(title = "报告管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Report report) {
        return toAjax(iReportService.save(report) ? 1 : 0);
    }

    /**
     * 修改报告管理
     */
    @PreAuthorize("@ss.hasPermi('file:report:edit')")
    @Log(title = "报告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Report report) {
        return toAjax(iReportService.updateById(report) ? 1 : 0);
    }

    /**
     * 删除报告管理
     */
    @PreAuthorize("@ss.hasPermi('file:report:remove')")
    @Log(title = "报告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iReportService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
